package com.example.yls.work_wuziqi;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cn.bmob.v3.Bmob;

public class MainActivity extends AppCompatActivity {
    private WuziPanel wuziPanel;
    private Button startView;
    private Button lastView;
    private Button lineNet;
    private TextView showQizi;
    private static final int VIEW_CODE = 100;
    private  boolean who = true;
    private int isGameOver;
    private int isWhiterWin;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
             who = (boolean)message.obj;
             isGameOver = message.arg1;
             isWhiterWin = message.arg2;
             if(message.what == VIEW_CODE) {
                 switch (isGameOver) {
                     case 1:
                           if(isWhiterWin==1) {
                               showQizi.setText("白棋赢");
                           }else{
                               showQizi.setText("黑棋赢");
                           }
                         break;
                     case 0:
                         if (who) {
                             showQizi.setText("白棋请下子");
                         } else {
                             showQizi.setText("黑棋请下子");
                         }
                         break;
                     default:
                         break;
                 }
             }
            return true;
        }
    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Bmob.initialize(this, "663dca6db560865a269b189e82c27b37");
        initView();
        showQizis();
        lastViews();
        lineNet();
    }

    private void lineNet() {
        lineNet = findViewById(R.id.lineNet_view);
        lineNet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final EditText et = new EditText(MainActivity.this);
                new AlertDialog.Builder(MainActivity.this).setTitle("请输入房间号")
                .setIcon(R.drawable.netimage)
                .setView(et)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,"没找到该房间号",Toast.LENGTH_SHORT).show();
                    }
                }).setNegativeButton("取消",null).show();
            }
        });
    }

    private void lastViews() {
        lastView = findViewById(R.id.last_view);
        lastView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                wuziPanel.lastView();
            }
        });
    }

    private void showQizis() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(100);
                        Message message = Message.obtain();
                        message.what = VIEW_CODE;
                        message.arg1 = wuziPanel.mIsGameOver ? 1 : 0 ;
                        message.arg2 = wuziPanel.mIsWhiteWinner ? 1 : 0;
                        message.obj = wuziPanel.mIsWhite;
                        handler.sendMessage(message);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    private void initView() {
        startView = findViewById(R.id.start_view);
        wuziPanel = findViewById(R.id.wuzipanel);
        showQizi = findViewById(R.id.show_qizi);
        startView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                wuziPanel.start();
            }
        });
    }
}
